<?php
class Admin_Model_Articles extends My_Model_Abstract
{
	protected $_mainTable = 'articles';

	protected $_idFieldName = 'p_articles';

	public function getCollection($numVente)
	{
	    $select = $this->getAdapter()->select()
	    ->from(
            $this->_mainTable,
            array('num_article', 'estimation', 'p_articles', 'desc_longue', 'numero', 'sous', 'adjuge', 'estimation', 'caisse')
        )
        ->join('categories', 'categories.p_categories = articles.f_categories', array('nom'))
        ->join('series', 'articles.f_series = series.p_series', array('titre'))
        ->where('articles.deleted = ?', 0)
        ->where('articles.f_ventes = ?', $numVente)
        ->order(array('caisse'));
	    return $select;
	    //return $this->getAdapter()->fetchAll($select);
	}

	public function getClients()
	{
	    $select = $this->getAdapter()->select()
	    ->from('clients', array('prenom', 'nom', 'p_clients'))
	    ->where('vendeur = ?', 1)
	    ->where('deleted = ?', 0)
	    ->order(array('nom', 'prenom'));

	    return $this->getAdapter()->fetchAll($select);
	}

	public function getAcheteur($clientAcheteur, $numVente)
	{
	    $select = $this->getAdapter()->select()
	    ->from('clients', array('prenom', 'nom'))
	    ->join('clients_ventes', 'clients_ventes.f_clients = clients.p_clients', array('num_client'))
	    ->where('p_clients = ?', $clientAcheteur ? $clientAcheteur : 0)
	    ->where('deleted = ?', 0)
	    ->where('f_ventes = ?', $numVente)
	    ->order(array('nom', 'prenom'));

	    return $this->getAdapter()->fetchRow($select);
	}

	public function getSeries()
	{
	    $select = $this->getAdapter()->select()
	    ->from('series', array('*'))
	    ->where('deleted = ?', 0)
	    ->where('archive = ?', 0)
	    ->order(array('titre'));

	    return $this->getAdapter()->fetchAll($select);
	}

	public function getCategories($numVente)
	{
	    $select = $this->getAdapter()->select()
	    ->from('categories', array('nom', 'p_categories'))
	    ->where('deleted = ?', 0)
	    ->where('f_ventes = ?', $numVente)
	    ->order(array('ordre', 'nom'));

	    return $this->getAdapter()->fetchAll($select);
	}

	public function getVentes()
	{
	    $select = $this->getAdapter()->select()
	    ->from('ventes', array('dtvente', 'p_ventes', 'titre'))
	    ->where('deleted = ?', 0)
	    ->order(array('dtvente'));

	    return $this->getAdapter()->fetchAll($select);
	}

	public function deleteOffrants()
	{
	    $where = $this->getAdapter()->quoteInto('f_articles = ?', $this->getId());
	    $this->getAdapter()->delete('articles_offrants', $where);
	    return $this;
	}
}